package cn.net.wangyixinwen.mvvm.vm;

import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import cn.net.wangyixinwen.api.ApiRepository;
import cn.net.wangyixinwen.bean.NewsBean;
import cn.net.wangyixinwen.bean.OrderBean;
import cn.net.wangyixinwen.bean.TypeBean;
import cn.net.wangyixinwen.bean.TypeNewsBean;
import cn.net.wangyixinwen.event.SingleLiveEvent;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;

/**
 * 分类
 */
public class TypeVM extends BaseViewModel {

    public SingleLiveEvent<List<TypeBean>> eventList = new SingleLiveEvent<>();

    public SingleLiveEvent<List<NewsBean>> newListEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<List<OrderBean.ListDTO>> orderListEvent = new SingleLiveEvent<>() ;

    public void getFenLei() {
        List<TypeBean> list = new ArrayList<>();
        list.add(new TypeBean("娱乐", "http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html"));
        list.add(new TypeBean("汽车", "http://c.m.163.com/nc/auto/list/5bmz6aG25bGx/0-20.html"));
        list.add(new TypeBean("平顶山", "http://c.3g.163.com/nc/article/local/5bmz6aG25bGx/0-20.html"));
        list.add(new TypeBean("头条", "http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html"));
        list.add(new TypeBean("订阅", "http://c.m.163.com/nc/special/S1471399123413.html"));
        eventList.setValue(list);
    }

    public void getTypeList(TypeBean bean) {
        if (Objects.equals(bean.getTypeName(), "汽车")) {
            ApiRepository.getTypeList1(bean.getUrl())
                    .safeSubscribe(new DisposableObserver<TypeNewsBean>() {
                        @Override
                        public void onNext(@NonNull TypeNewsBean typeNewsBean) {
                            newListEvent.setValue(typeNewsBean.getList());
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            ToastUtils.showShort(e.getMessage());
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        } else {
            ApiRepository.getTypeList(bean.getUrl())
                    .safeSubscribe(new DisposableObserver<Map<String, List<NewsBean>>>() {
                        @Override
                        public void onNext(@NonNull Map<String, List<NewsBean>> map) {
                            List<NewsBean> list = new ArrayList<>();
                            for (String key : map.keySet()) {
                                list.addAll(map.get(key));
                            }
                            newListEvent.setValue(list);
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            Log.d("数据获取失败", "onError: " + e.getMessage());
                            ToastUtils.showShort("网络请求失败");
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }



    }
}
